package com.live.activity;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings.Secure;

import com.live.common.Constant;
import com.live.service.PushService;
import com.live.task.LiveMatch;
import com.live.task.LiveWelcome;
import com.live.tools.LiveTools;

public class LiveWelcomeActivity extends LiveBaseActivity {

	static final int DATE_DIALOG_ID = 0;

	private final Handler handler = new Handler();

	private Timer t = new Timer();

	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	private String today;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.live_welcome);

		t.schedule(new TimerTask() {
			public void run() {
				handler.post(new Runnable() {
					public void run() {
						// 网络连接判断
						if (netIsOk) {

							today = sdf.format(System.currentTimeMillis());

							String url = Constant.SITE_URL + "live_match.php?locale=" + LiveTools.getLocaleLanguage()
									+ "&matchTime=" + today + "&matchStatus=全部&matchCategory=0";

							LiveMatch liveMatch = new LiveMatch(context, null, today);
							liveMatchList = liveMatch.getServerData(url);

							// 记录访问者信息
							LiveWelcome liveWelcome = new LiveWelcome(context, deviceID);
							liveWelcome.execute();

							// 初始化头像缓存文件夹
							File file = new File(Constant.ALBUM_TEMP_PATH);
							LiveTools.deleteDir(file);

							Intent intent = new Intent();

							intent.setClass(context, LiveMatchActivity.class);

							context.startActivity(intent);
							finish();
						}
					}
				});
			}
		}, 500);

		// 启动推送服务
		deviceID = Secure.getString(this.getContentResolver(), Secure.ANDROID_ID);
		// Toast.makeText(this, deviceID, Toast.LENGTH_LONG).show();
		if (store.getString("userId", null) != null) {
			if (!LiveTools.isServiceRunning(context, "com.live.service.PushService")) {
				Editor editor = getSharedPreferences(PushService.TAG, MODE_PRIVATE).edit();
				editor.putString(PushService.PREF_DEVICE_ID, deviceID);
				editor.commit();
				PushService.actionStart(context);
			}
		} else {
			PushService.actionStop(context);
		}
	}
}